#include <stdlib.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define STACK_INCREATMENT 10
#define MAX_SIZE 100
typedef int Status;
typedef int SElemType;

typedef struct SqStack // 课本的栈底不存数据
{
    SElemType *base;
    SElemType *top;
    int stacksize;
} SqStack;

Status InitStack(SqStack *stack);             // 初始化栈
Status push(SqStack *stack, SElemType e);     // 压栈
Status Get_top(SqStack *stack, SElemType *e); // 获取栈顶
Status is_empty(SqStack *stack);              // 判断是否是空
Status pop(SqStack *stack, SElemType *e);     // 弹出栈顶
int Get_length(SqStack *stack);               // 获取栈内元素数
Status clear_SqStack(SqStack *stack);         // 清空栈并输出站内所有元素

Status InitStack(SqStack *stack)
{
    stack->base = (SElemType *)malloc(sizeof(SElemType) * MAX_SIZE);
    stack->stacksize = MAX_SIZE;
    stack->top = stack->base;
    return OK;
}

Status push(SqStack *stack, SElemType e)
{
    stack->top++; // 栈底不放
    *stack->top = e;
}

Status clear_SqStack(SqStack *stack)
{
    while (stack->top != stack->base)
    {
        printf("%d ", *(stack->top));
        stack->top--;
    }
    printf("\n");
    return OK;
}

Status is_empty(SqStack *stack)
{
    if (stack->top == stack->base)
    {
        return TRUE;
    }
    return FALSE;
}

Status Get_top(SqStack *stack, SElemType *e)
{
    if (stack->top == stack->base)
    {
        return ERROR;
    }
    else
    {
        *e = *stack->top;
    }
    return OK;
}

int Get_length(SqStack *stack)
{
    return stack->top - stack->base;
}

Status pop(SqStack *stack, SElemType *e)
{
    if (stack->top == stack->base)
    {
        return ERROR;
    }
    *e = *stack->top;
    stack->top--;
    return OK;
}
